package com.x.xiaoshuo.manager;

import com.x.mvp.utils.DensityUtil;
import com.x.service.entity.Version;
import com.x.xiaoshuo.Constant;
import com.x.xiaoshuo.utils.SharedPreferencesUtil;
import com.x.xiaoshuo.utils.ToastUtils;

public class SettingManager {

    private volatile static SettingManager manager;

    public static SettingManager getInstance() {
        return manager != null ? manager : (manager = new SettingManager());
    }


    public void saveVersion(Version version) {
        SharedPreferencesUtil.getInstance().putObject(getVersionKey(), version);
    }

    public Version getVersion() {
        return SharedPreferencesUtil.getInstance().getObject(getVersionKey(), Version.class);
    }

    private String getVersionKey() {
        return "-VERSION";
    }

    public void setLastVersionCode(int code) {
        SharedPreferencesUtil.getInstance().putInt(getVersionCodeKey(), code);
    }

    public int getLastVersionCode() {
        return SharedPreferencesUtil.getInstance().getInt(getVersionCodeKey(), 0);
    }

    private String getVersionCodeKey() {
        return "-VERSION_CODE";
    }

    public void saveScreenOrientation(int orientation) {
        SharedPreferencesUtil.getInstance().putInt(getScreenOrientationKey(), orientation);
    }

    public int getScreenOrientation() {
        return SharedPreferencesUtil.getInstance().getInt(getScreenOrientationKey(), 0);
    }

    private String getScreenOrientationKey() {
        return "-SCREENORIENTATION";
    }


    public void saveFlipStyle(int flip) {
        // 书籍对应
        SharedPreferencesUtil.getInstance().putInt(getFlipStyleKey(), flip);
    }

    public int getFlipStyle() {
        return SharedPreferencesUtil.getInstance().getInt(getFlipStyleKey(), 0);
    }

    private String getFlipStyleKey() {
        return "-FlipStyle";
    }

    public void saveShelfOrder(int orderType) {
        // 书籍对应
        SharedPreferencesUtil.getInstance().putInt(getShelfOrderKey(), orderType);
    }

    public int getShelfOrder() {
        return SharedPreferencesUtil.getInstance().getInt(getShelfOrderKey(), 0);
    }

    private String getShelfOrderKey() {
        return "-shelfOrder";
    }


    public void saveFontSize(String bookId, int fontSizePx) {
        // 书籍对应
        SharedPreferencesUtil.getInstance().putInt(getFontSizeKey(bookId), fontSizePx);
    }


    public void saveFontSize(int fontSizePx) {
        saveFontSize("", fontSizePx);
    }

    public int getReadFontSize(String bookId) {
        return SharedPreferencesUtil.getInstance().getInt(getFontSizeKey(bookId), 20);
    }

    public int getReadFontSize() {
        return getReadFontSize("");
    }

    private String getFontSizeKey(String bookId) {
        return bookId + "-readFontSize";
    }

    public int getReadBrightness() {
        return DensityUtil.getScreenBrightness();
    }


    public void saveReadBrightness(int percent) {
        if (percent > 100) {
            ToastUtils.showToast("saveReadBrightnessErr CheckRefs");
            percent = 100;
        }
        SharedPreferencesUtil.getInstance().putInt(getLightnessKey(), percent);
    }

    private String getLightnessKey() {
        return "readLightness";
    }

    public synchronized void saveReadProgress(String bookId, int currentChapter, int m_mbBufBeginPos, int m_mbBufEndPos) {
        SharedPreferencesUtil.getInstance()
                .putInt(getChapterKey(bookId), currentChapter)
                .putInt(getStartPosKey(bookId), m_mbBufBeginPos)
                .putInt(getEndPosKey(bookId), m_mbBufEndPos);
    }


    public int[] getReadProgress(String bookId) {
        int lastChapter = SharedPreferencesUtil.getInstance().getInt(getChapterKey(bookId), 1);
        int startPos = SharedPreferencesUtil.getInstance().getInt(getStartPosKey(bookId), 0);
        int endPos = SharedPreferencesUtil.getInstance().getInt(getEndPosKey(bookId), 0);

        return new int[]{lastChapter, startPos, endPos};
    }

    public void removeReadProgress(String bookId) {
        SharedPreferencesUtil.getInstance()
                .remove(getChapterKey(bookId))
                .remove(getStartPosKey(bookId))
                .remove(getEndPosKey(bookId));
    }

    private String getChapterKey(String bookId) {
        return bookId + "-chapter";
    }

    private String getStartPosKey(String bookId) {
        return bookId + "-startPos";
    }

    private String getEndPosKey(String bookId) {
        return bookId + "-endPos";
    }

    private String getBookMarksKey(String bookId) {
        return bookId + "-marks";
    }

    public void saveReadTheme(int theme) {
        SharedPreferencesUtil.getInstance().putInt("readTheme", theme);
    }

    public int getReadTheme() {
        if (SharedPreferencesUtil.getInstance().getBoolean(Constant.ISNIGHT, false)) {
            return ThemeManager.NIGHT;
        }
        return SharedPreferencesUtil.getInstance().getInt("readTheme", 3);
    }


    public void saveVolumeFlipEnable(boolean enable) {
        SharedPreferencesUtil.getInstance().putBoolean("volumeFlip", enable);
    }

    public boolean isVolumeFlipEnable() {
        return SharedPreferencesUtil.getInstance().getBoolean("volumeFlip", true);
    }

    public void saveAutoBrightness(boolean enable) {
        SharedPreferencesUtil.getInstance().putBoolean("autoBrightness", enable);
    }

    public boolean isAutoBrightness() {
        return SharedPreferencesUtil.getInstance().getBoolean("autoBrightness", false);
    }

    public boolean isNoneCover() {
        return SharedPreferencesUtil.getInstance().getBoolean("isNoneCover", false);
    }

    public void saveNoneCover(boolean isNoneCover) {
        SharedPreferencesUtil.getInstance().putBoolean("isNoneCover", isNoneCover);
    }
}
